package edu.guigu.networkdemomvvm.single;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import edu.guigu.networkdemomvvm.R;
import edu.guigu.networkdemomvvm.databinding.ActivitySingleViewBinding;

public class SingleViewActivity extends AppCompatActivity implements View.OnClickListener{

    private ActivitySingleViewBinding binding;
    // TODO: ViewModel到仓库去请求数据的VM(内部是LiveData数据)
    private UserRequestViewModel requestViewModel;
    // TODO: ViewModel(与视图绑定的数据)
    private UserStateViewModel stateViewModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EdgeToEdge.enable(this);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_single_view);
        binding.setLifecycleOwner(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        binding.btnLoad.setOnClickListener(this);

        stateViewModel = new ViewModelProvider(this).get(UserStateViewModel.class);
        binding.setViewModel(stateViewModel);

        requestViewModel = new ViewModelProvider(this).get(UserRequestViewModel.class);
        showProgressDialog();
        requestViewModel.remoteGetUser(index++);
        requestViewModel.responeLiveData.observe(this, new Observer<SingleUser>() {
            @Override
            public void onChanged(SingleUser user) {
                stateViewModel.updateUser(user);
                hideProgressDialog();
            }
        });
        requestViewModel.failuerLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(SingleViewActivity.this, s, Toast.LENGTH_LONG).show();
                hideProgressDialog();
            }
        });
    }

    int index = 1;
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_load) {
            showProgressDialog();
            requestViewModel.remoteGetUser(index++);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }

    private ProgressDialog progressDialog;

    private void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("正在加载中...");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }

    private void hideProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }
}